Outline

  1. make a folder
  2. drag starwars.csv
  3. Make existing folder and RStudio project
  4. Open an R Markdown Notebook
  5. library(tidyverses)
  6. read_csv(starwars.csv)
  7. ggplot(data = starwars, aes(hair_color)) + geom_bar()
  8. summary(starwars)
  9. skimr(starwars)
  10. left_join(starwars, fivethirtyeight)
  11. Transform data: five dplyr verbs …
  12. count / group_by & summarize
  13. Make barchart an interactive ggplotly
  14. Quick Linear Regression
  15. Save Notebook report, and as MSWord file
library(tidyverse)
library(skimr)
library(plotly)
library(broom)
data("starwars")
plot <- ggplot(data = starwars, aes(hair_color)) + 
  geom_bar()
plot

skim(starwars)
-- Data Summary ------------------------
                           Values  
Name                       starwars
Number of rows             87      
Number of columns          14      
_______________________            
Column type frequency:             
  character                8       
  list                     3       
  numeric                  3       
________________________           
Group variables            None    

-- Variable type: character ---------------------------------------------------------------------------------------------
# A tibble: 8 x 8
  skim_variable n_missing complete_rate   min   max empty n_unique whitespace
* <chr>             <int>         <dbl> <int> <int> <int>    <int>      <int>
1 name                  0         1         3    21     0       87          0
2 hair_color            5         0.943     4    13     0       12          0
3 skin_color            0         1         3    19     0       31          0
4 eye_color             0         1         3    13     0       15          0
5 sex                   4         0.954     4    14     0        4          0
6 gender                4         0.954     8     9     0        2          0
7 homeworld            10         0.885     4    14     0       48          0
8 species               4         0.954     3    14     0       37          0

-- Variable type: list --------------------------------------------------------------------------------------------------
# A tibble: 3 x 6
  skim_variable n_missing complete_rate n_unique min_length max_length
* <chr>             <int>         <dbl>    <int>      <int>      <int>
1 films                 0             1       24          1          7
2 vehicles              0             1       11          0          2
3 starships             0             1       17          0          5

-- Variable type: numeric -----------------------------------------------------------------------------------------------
# A tibble: 3 x 11
  skim_variable n_missing complete_rate  mean    sd    p0   p25   p50   p75  p100 hist 
* <chr>             <int>         <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 height                6         0.931 174.   34.8    66 167     180 191     264 ▁▁▇▅▁
2 mass                 28         0.678  97.3 169.     15  55.6    79  84.5  1358 ▇▁▁▁▁
3 birth_year           44         0.494  87.6 155.      8  35      52  72     896 ▇▁▁▁▁
  1. summarize(gender)
starwars %>% 
  summary(gender)
     name               height           mass          hair_color         skin_color         eye_color        
 Length:87          Min.   : 66.0   Min.   :  15.00   Length:87          Length:87          Length:87         
 Class :character   1st Qu.:167.0   1st Qu.:  55.60   Class :character   Class :character   Class :character  
 Mode  :character   Median :180.0   Median :  79.00   Mode  :character   Mode  :character   Mode  :character  
                    Mean   :174.4   Mean   :  97.31                                                           
                    3rd Qu.:191.0   3rd Qu.:  84.50                                                           
                    Max.   :264.0   Max.   :1358.00                                                           
                    NA's   :6       NA's   :28                                                                
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
                                                                                                              
   birth_year         sex               gender           homeworld           species         
 Min.   :  8.00   Length:87          Length:87          Length:87          Length:87         
 1st Qu.: 35.00   Class :character   Class :character   Class :character   Class :character  
 Median : 52.00   Mode  :character   Mode  :character   Mode  :character   Mode  :character  
 Mean   : 87.57                                                                              
 3rd Qu.: 72.00                                                                              
 Max.   :896.00                                                                              
 NA's   :44                                                                                  
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
 films.Length  films.Class  films.Mode vehicles.Length  vehicles.Class  vehicles.Mode
 5          -none-     character       2          -none-     character               
 6          -none-     character       0          -none-     character               
 7          -none-     character       0          -none-     character               
 4          -none-     character       0          -none-     character               
 5          -none-     character       1          -none-     character               
 3          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 6          -none-     character       1          -none-     character               
 3          -none-     character       2          -none-     character               
 2          -none-     character       0          -none-     character               
 5          -none-     character       1          -none-     character               
 4          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 3          -none-     character       1          -none-     character               
 1          -none-     character       0          -none-     character               
 5          -none-     character       0          -none-     character               
 5          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       1          -none-     character               
 3          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       1          -none-     character               
 1          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 3          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 2          -none-     character       1          -none-     character               
 2          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       1          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 1          -none-     character       0          -none-     character               
 starships.Length  starships.Class  starships.Mode
 2          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 5          -none-     character                  
 3          -none-     character                  
 0          -none-     character                  
 2          -none-     character                  
 2          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 1          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 0          -none-     character                  
 [ reached getOption("max.print") -- omitted 16 rows ]

Load more data

favorability <- read_csv("https://raw.githubusercontent.com/libjohn/intro2r-code/master/data/538_favorability_popularity.csv",
                         ,skip = 11)
Parsed with column specification:
cols(
  name = col_character(),
  fav_rating = col_double()
)
  1. left_join(starwars, fivethirtyeight)
starwars %>% 
  left_join(favorability, by = "name")
  1. Transform data: five dplyr verbs …
starwars %>% 
  select(name, gender, hair_color)
starwars %>% 
  filter(gender == "feminine")
starwars %>% 
  arrange(desc(height), desc(name))
starwars %>%
  drop_na(mass) %>% 
  select(name, mass) %>% 
  mutate(big_mass = mass * 2)
  1. count / group_by & summarize
starwars %>% 
  count(gender)
starwars %>% 
  drop_na(mass) %>% 
  summarise(sum(mass))
starwars %>% 
  drop_na(height) %>% 
  group_by(gender, species) %>% 
  summarise(mean_height = mean(height), total = n()) %>% 
  arrange(-total) %>%
  drop_na(gender) %>% 
  filter(total > 1)
`summarise()` regrouping output by 'gender' (override with `.groups` argument)
ggplotly(plot)
model <- lm(mass ~ height, data = starwars %>% filter(mass < 500))
model

Call:
lm(formula = mass ~ height, data = starwars %>% filter(mass < 
    500))

Coefficients:
(Intercept)       height  
   -32.5408       0.6214  
tidy(model)
glance(model)
augment(model)
starwars %>% 
  filter(mass < 500) %>%
  ggplot(aes(height, mass)) +
  geom_point() +
  geom_smooth(method = "lm")

Render reports

LS0tDQp0aXRsZTogIlF1aWNrc3RhcnQgZGVtbyINCmF1dGhvcjogIkpvaG4gTGl0dGxlIg0KZGF0ZTogImByIFN5cy5EYXRlKClgIg0Kb3V0cHV0OiBodG1sX25vdGVib29rDQotLS0NCg0KIyMgT3V0bGluZQ0KDQoxLiBtYWtlIGEgZm9sZGVyDQoyLiBkcmFnIHN0YXJ3YXJzLmNzdg0KMy4gTWFrZSBleGlzdGluZyBmb2xkZXIgYW5kIFJTdHVkaW8gcHJvamVjdA0KNC4gT3BlbiBhbiBSIE1hcmtkb3duIE5vdGVib29rDQo1LiBgbGlicmFyeSh0aWR5dmVyc2VzKWANCjYuIGByZWFkX2NzdihzdGFyd2Fycy5jc3YpYA0KNy4gYGdncGxvdChkYXRhID0gc3RhcndhcnMsIGFlcyhoYWlyX2NvbG9yKSkgKyBnZW9tX2JhcigpYCANCjguIGBzdW1tYXJ5KHN0YXJ3YXJzKWANCjkuIGBza2ltcihzdGFyd2FycylgDQoxMC4gYGxlZnRfam9pbihzdGFyd2FycywgZml2ZXRoaXJ0eWVpZ2h0KWANCjEyLiBUcmFuc2Zvcm0gZGF0YTogZml2ZSBkcGx5ciB2ZXJicyAuLi4NCjEzLiBgY291bnRgIC8gYGdyb3VwX2J5YCAmIGBzdW1tYXJpemVgDQoxNC4gTWFrZSBiYXJjaGFydCBhbiBpbnRlcmFjdGl2ZSBnZ3Bsb3RseQ0KMTUuIFF1aWNrIExpbmVhciBSZWdyZXNzaW9uDQoxNi4gU2F2ZSBOb3RlYm9vayByZXBvcnQsIGFuZC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